package tutorial.lite_mod.system_use.search_msg;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import public_util.model.*;
import public_util.pack_4_stable.tools.DateTool;
import system_api_4_trade.dao.ApiPublicMiddleTableMapper;
import system_api_4_trade.util.sql.InsertTool;
import system_api_4_trade.util.tool_4_handle.Tool_4_ServletApi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 采用静态变量的方式来存储相关的IP,若干分钟后,批量写入进去
 * 传递特殊参数, 可以演示的模板部分
 * 后面大部分的配置, 都是前端自己传过来比较稳妥
 * 每个传参对应一个相应的方法
 */
public class TemplateUtilApi {

    private static Logger logger = Logger.getLogger(TemplateUtilApi.class);


    /**
     * 工具零件方法
     * 组装方法: 主要是在before中,调用的时候判断IP地址使用了多少次
     * 根据这个零件来拼装
     * 将相关的id进行组装一把
     *
     * @return
     */
    public static ReturnData pre_cmd_para_save_and_show_ip(HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db, ReturnData resultData)
    {
        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        return null;
    }




    /**
     * 用户查看详情后, 数据保存,作为兴趣分析点
     * @param listGoods
     * @param request
     * @param session
     * @param params2db
     * @param resultData
     * @return
     */
    public static ReturnData pre_cmd_para_save_display_data(List<PublicGoodsInfo> listGoods, HttpServletRequest request, SqlSession session, HashMap<String, Object> params2db, ReturnData resultData)
    {

        ApiPublicMiddleTableMapper middleTableMapper = session.getMapper(ApiPublicMiddleTableMapper.class);
        String pre_cmd_para_show_fei_e_print = request.getParameter("pre_cmd_para_save_display_data");
        if ("true".equals(pre_cmd_para_show_fei_e_print))
        {
            if (listGoods != null && listGoods.size() == 1)
            {
                PublicGoodsInfo goodsInfo = listGoods.get(0);
                HttpSession httpSession = request.getSession();
                HashMap<String, Object> paraPrice = new HashMap<String, Object>();
                PublicUserInfo userInfo = (PublicUserInfo) Tool_4_ServletApi.getAttribute(null, httpSession, Const4PublicDataBase.MEM_4_self_public_user_info);
                if (userInfo != null)
                {
                    paraPrice.put("id_public_user_info",userInfo.getId_public_user_info());//用户
                    paraPrice.put("buyer_id",userInfo.getId_public_agent_info());//门店
                    paraPrice.put("id_public_goods_info",goodsInfo.getId_public_goods_info());//商品ID
                }

                paraPrice.put("table_id_father",goodsInfo.getId_public_goods_type());//
                paraPrice.put("table_id_child",goodsInfo.getType_child());//
                paraPrice.put("mid_table_remark_001",goodsInfo.getType_name());//
                paraPrice.put("mid_table_remark_002",goodsInfo.getChild_name());//
                paraPrice.put("mid_table_remark_003",goodsInfo.getGoods_name());//

                PublicGoodsSupply4Agent agentGoods = goodsInfo.getPublicGoodsSupply4Agent();
                if (agentGoods != null)
                {
                    paraPrice.put("id_public_agent_info",agentGoods.getSeller_id());//
                }

                insert_search_msg_table(session, middleTableMapper, paraPrice);
                session.commit();
            }
        }

        return null;
    }




    /**
     * 日志信息的插入
     */
    public static void insert_search_msg_table(SqlSession session , ApiPublicMiddleTableMapper midMapper, HashMap<String, Object> msgData)
    {
        msgData.put(Const4PublicDataBase.PARA_type_middle_app, 80001);
        msgData.put(Const4PublicDataBase.PARA_type_inner_app, 1001);
        msgData.put(Const4PublicDataBase.PARA_is_online, 0);
        msgData.put(Const4PublicDataBase.PARA_time_insert, DateTool.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        HashMap<String, Object> selMap = InsertTool.paramap2Insert(msgData, "public_middle_table_system_use_search_msg");
        midMapper.insertPublicMiddleTable(selMap);
        session.commit();
    }



}
